home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-06-26 | 3.9 KB | 79 lines | [TEXT/GEOL] |
- Item 2273449 12-June-89 13:14
-
- From: ALGER Alger, Jeff
-
- To: MACAPP.TECH$ MACAPP Tech
-
- Sub: Re - Comparing Objects
-
- Alan, Chuck, Keith, et al,
-
- Add my vote to those asking for TObject.Compare and TObject.IsSame. I agree
- with Chuck Lins that identity should be separated from ordering by having a
- distinct IsSame method. Ordering is (usually) a property of the set, rather
- than the class (although more about that later,) while identity is a property
- of the class.
-
- Some careful attention to semantics is required in order to make a
- TObject.IsSame method useful and meaningful. In particular, it is important to
- preserve reflexivity and transitivity in an IsSame method. Suppose we have
- three objects x, y, and z of classes A, B, and C, respectively. Reflexivity
- should guarantee that when x.IsSame(y) is true, y.IsSame(x) is true.
- Unfortunately, this is not always possible to guarantee. If B is a subclass of
- A which overrides IsSame, reflexivity in this strict sense is not guaranteed.
-
- However, if you interpret things a little different, reflexivity holds.
- Interpret x.IsSame(y) as follows:
-
- 1. If class B is the same as class A, x.IsSame(y) must implemented to be
- reflexive and transitive.
- 2. If class B does not have class A as an ancestor, x.IsSame(y) is undefined.
- 3. If class B does have class A as an ancestor, x.IsSame(y) is equivalent to
- x.IsSame(A(y)).
-
- This means that reflexivity is always guaranteed whenever IsSame is
- well-defined. As to transivitity, x.IsSame(y) and y.IsSame(z) clearly need not
- imply x.IsSame(z), but x.IsSame(A(y)) and A(y).IsSame(A(z)) just as clearly
- will imply x.IsSame(A(z)). The bottom line is that reflexivity and
- transitivity are preserved as long as all of the objects under discussion are
- coerced to a common class before applying the IsSame method.
-
- As to ordering, a set can be unordered, partially ordered, or fully ordered.
- An unordered set, such as a hash table, does not support the notion of a
- successor or predecessor to a given object, while an ordered set does. In a
- partially ordered set, there may be non-unique keys, which is to say, two
- distinct objects may compare as “equal” for purposes of set ordering, while in
- a fully ordered set two objects can compare as “equal” only if they are the
- same object. One object can be an element of any number of sets, each of which
- may have its own definition of “order” or no order at all.
-
- On the surface, this argues for keeping IsSame in TObject, but Compare as a
- method of the set. However, in practice there are many object classes which
- have an obvious natural ordering. In such cases, it makes sense to provide a
- Compare method for the element class, rather than the set.
-
- I see no reason to avoid a TObject.Compare method just because it will not
- always be used. As has been pointed out, one of the few tools available to
- deal with the lack of multiple inheritance in Object Pascal is a rich set of
- primitive methods of TObject. It costs next to nothing to provide an
- overridable TObject.Compare and it is, at times, useful and meaningful, so why
- not? TObject.Compare does not rule out set-specific ordering. It is usually
- applicable in the simpler cases, and where it does apply one avoids an entire
- new class (e.g., a subclass of TSortedList) just to implement a simple sorted
- set. Let’s keep simple things simple to implement: that is, after all, the
- spirit of MacApp.
-
- Both IsSame and Compare should be able to return “kIncomparable”, as Chuck
- suggests, whenever class coercion is not possible. Also, if both are present,
- the default implementation of IsSame can compare object handles without running
- afoul of Keith’s concern about consistency across invocations of the
- application. Compare should compare only that which is common to TObject:
- class ID and object size, both of which are consistent and result in partially
- ordered sets.
-
- Further thoughts?
-
- Jeff Alger
- Peat Marwick Main & Co.
-
-